#include "readingview.h"

ReadingView::ReadingView()
{
    pack_start(m_calendar);
    m_calendar.signal_month_changed().connect(sigc::mem_fun(*this,&ReadingView::on_month_changed));
    m_calendar.signal_day_selected().connect(sigc::mem_fun(*this,&ReadingView::on_day_selected));
    Gtk::Button* btn1=Gtk::manage(new Gtk::Button("Expired items"));
    pack_start(*btn1,Gtk::PACK_SHRINK);
    Gtk::Button* btn2=Gtk::manage(new Gtk::Button("10 days expired items"));
    pack_start(*btn2,Gtk::PACK_SHRINK);
    Gtk::Button* btn3=Gtk::manage(new Gtk::Button("future reading items"));
    pack_start(*btn3,Gtk::PACK_SHRINK);
    Gtk::Button* btn4=Gtk::manage(new Gtk::Button("Items Read History"));
    pack_start(*btn4,Gtk::PACK_SHRINK);
    btn1->signal_clicked().connect(sigc::mem_fun(*this,&ReadingView::on_expiredbutton_click));
    btn2->signal_clicked().connect(sigc::mem_fun(*this,&ReadingView::on_10daysbutton_click));
    btn3->signal_clicked().connect(sigc::mem_fun(*this,&ReadingView::on_futurebutton_click));
    btn4->signal_clicked().connect(sigc::mem_fun(*this,&ReadingView::on_historybutton_click));
}

ReadingView::~ReadingView()
{
    //dtor
}
void ReadingView::set_documents(sdms::documents* d)
{
    mydoc=d;
}
void ReadingView::set_filterview(FilterView* f)
{
    filterview=f;
}
void ReadingView::on_expiredbutton_click()
{
    sdms::ReadingFilter f;
    f.set_documents(mydoc);
    f.set_id(1);
    f.eval();
    filterview->clear();
    filterview->add(f);
}
void ReadingView::on_10daysbutton_click()
{
    sdms::ReadingFilter f;
    f.set_documents(mydoc);
    f.set_id(2);
    f.eval();
    filterview->clear();
    filterview->add(f);
}

void ReadingView::on_futurebutton_click()
{
    sdms::ReadingFilter f;
    f.set_documents(mydoc);
    f.set_id(3);
    f.eval();
    filterview->clear();
    filterview->add(f);
}
void ReadingView::on_historybutton_click()
{
    sdms::ReadingFilter f;
    f.set_documents(mydoc);
    f.set_id(4);
    f.eval();
    filterview->clear();
    filterview->add(f);
}
void ReadingView::on_month_changed()
{
    m_calendar.clear_marks();
    std::vector<sdms::reading> r=mydoc->getmonthreading(m_calendar.property_year(),m_calendar.property_month()+1);
    for(int i=0; i<r.size(); i++)
    {
        m_calendar.mark_day(utils::fromString<int>(r[i].plan.substr(8,2)));
    }
}
void ReadingView::on_day_selected()
{
    guint year,month,day;
    m_calendar.get_date(year,month,day);
    month++;
    std::string date=utils::toString(year);
    date+="-";
    if (month<10) date+="0";
    date+=utils::toString(month);
    date+="-";
    if (day<10) date+="0";
    date+=utils::toString(day);
    sdms::ReadingFilter f;
    f.set_documents(mydoc);
    f.set_id(5);
    f.set_name(date);
    f.eval();
    filterview->clear();
    filterview->add(f);
}
void ReadingView::mark_current_month()
{
    time_t c;
    time(&c);
    struct tm* ptm=localtime(&c);
    std::vector<sdms::reading> r=mydoc->getmonthreading(ptm->tm_year+1900,ptm->tm_mon+1);
    for(int i=0; i<r.size(); i++)
    {
        m_calendar.mark_day(utils::fromString<int>(r[i].plan.substr(8,2)));
    }
}
void ReadingView::update()
{
    on_month_changed();
}

